package com.wthink.newMain.settings.config

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.FragmentBasicConfingBinding
import com.wthink.newMain.settings.config.inner.basic.BasicFragment
import com.wthink.newMain.settings.config.inner.NetConfigNewFragment
import com.wthink.newMain.settings.config.inner.calendar.CalendarSettingsFragment
import com.wthink.newMain.settings.config.inner.reboot.RebootFragment
import com.wthink.newMain.settings.config.inner.userConfig.UserConfigFragment

class BasicConfigFragment : Fragment() {

    private val viewModel by viewModels<BasicConfigViewmodel>()

    private lateinit var binding: FragmentBasicConfingBinding

    var basicFragment : BasicFragment? = null
    var netConfigNewFragment : NetConfigNewFragment? = null
    var userConfigFragment : UserConfigFragment? = null
    var calendarSettingsFragment : CalendarSettingsFragment? = null
    var rebootFragment:RebootFragment? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_basic_confing,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        basicFragment =
            BasicFragment()
        //netConfigNewFragment = NetConfigNewFragment()
        userConfigFragment = UserConfigFragment()
        calendarSettingsFragment =  CalendarSettingsFragment()
        rebootFragment = RebootFragment()

        activity!!.supportFragmentManager
            .beginTransaction()
            .replace(R.id.innerContent, basicFragment!!)
            .commit()

        binding.baseConfig.isSelected = true

        binding.baseConfig.setOnClickListener {
            addFragment(BasicConfigFragment())
            binding.baseConfig.isSelected = true
            binding.rebootConfin.isSelected = false
            binding.uSerConfin.isSelected = false
            binding.netConfin.isSelected = false
            binding.timeConfin.isSelected = false
        }

/*
        binding.netConfin.setOnClickListener {
            addFragment(netConfigNewFragment!!)
            binding.baseConfig.isSelected = false
            binding.rebootConfin.isSelected = false
            binding.uSerConfin.isSelected = false
            binding.netConfin.isSelected = true
            binding.timeConfin.isSelected = false
        }
*/
        binding.uSerConfin.setOnClickListener {
            addFragment(userConfigFragment!!)
            binding.baseConfig.isSelected = false
            binding.rebootConfin.isSelected = false
            binding.uSerConfin.isSelected = true
            binding.netConfin.isSelected = false
            binding.timeConfin.isSelected = false
        }

        binding.timeConfin.setOnClickListener {
            addFragment(CalendarSettingsFragment())
            binding.baseConfig.isSelected = false
            binding.rebootConfin.isSelected = false
            binding.uSerConfin.isSelected = false
            binding.netConfin.isSelected = false
            binding.timeConfin.isSelected = true
        }
        binding.rebootConfin.setOnClickListener {
            addFragment(rebootFragment!!)
            binding.baseConfig.isSelected = false
            binding.rebootConfin.isSelected = true
            binding.uSerConfin.isSelected = false
            binding.netConfin.isSelected = false
            binding.timeConfin.isSelected = false
        }
    }

    private fun addFragment(fragment: Fragment) {
        activity!!.supportFragmentManager
            .beginTransaction()
            //.setCustomAnimations(R.anim.design_bottom_sheet_slide_in, R.anim.design_bottom_sheet_slide_out)
            .replace(R.id.innerContent, fragment, fragment.javaClass.simpleName)
          //  .addToBackStack(fragment.javaClass.simpleName)
            .commit()
    }

}